//
//  CalculateWayViewController.m
//  YunGou
//
//  Created by macbook on 16/5/24.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "CalculateWayViewController.h"
#import "CalculateFormulaView.h"
#import "CalculateHeaderView1.h"
#import "CalculateHeaderView2.h"
#import "CalculateWayModel.h"
#import "CalculateWayCell.h"
#import <MJRefresh/MJRefresh.h>

@interface CalculateWayViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) CalculateFormulaView *calculateFormulaView;
@property (nonatomic,strong) UIView *bgView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation CalculateWayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(0, 0, 60, 25);
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"jsxq_jsgs_kuang"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(calculateClick) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:@"计算公式" forState:UIControlStateNormal];
    [rightBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
    rightBtn.titleLabel.textColor = ThemeColor;
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
    NSLog(@"%@",_goodsId);
    
    _dataArray = [NSMutableArray array];
    [NetWorkTool calculateInfoWithGoodsId:_goodsId success:^(id object) {
        if ([object[@"code"]integerValue] == 1) {
            for (NSDictionary *dict in object[@"data"]) {
                CalculateWayModel *model = [CalculateWayModel modelWithDict:dict];
                [_dataArray addObject:model];
            }
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
    _tableView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
        [_tableView.mj_header endRefreshing];
    }];
    
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [_tableView.mj_footer endRefreshing];
    }];

}

- (void)calculateClick{
    [self.tabBarController.view addSubview:self.bgView];
    [self.tabBarController.view addSubview:self.calculateFormulaView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_dataArray.count <= 5) {
        if (section == 0) {
            return _dataArray.count;
        }else{
            return 0;
        }
    }else if (_dataArray.count > 5 && _dataArray.count <= 10){
        if (section == 0) {
            return 5;
        }else if (section == 1){
            return 0;
        }else{
            return _dataArray.count - 5;
        }
    }else{
        if (section == 1) {
            return _dataArray.count - 10;
        }return 5;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CalculateWayCell *cell = nil;
    if (indexPath.section == 0 || indexPath.section == 2) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
    }
    cell.calculateModel = _dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01f;
    }else if(section == 1){
        return 150;
    } return 64;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        CalculateHeaderView1 *headerView = [CalculateHeaderView1 nibInitializtion];
        return headerView;
    }else if (section == 2){
        CalculateHeaderView2 *headerView = [CalculateHeaderView2 nibInitializtion];
        return headerView;
    }
    return nil;
}

#pragma -mark 懒加载
- (CalculateFormulaView *)calculateFormulaView{
    if (!_calculateFormulaView) {
        _calculateFormulaView = [CalculateFormulaView nibInitializtion];
        _calculateFormulaView.frame = CGRectMake(0, 0, 240, 320);
        _calculateFormulaView.center = self.tabBarController.view.center;
        __weak typeof(self) ws = self;
        _calculateFormulaView.calculateFormulaViewBlock = ^{
            [ws.bgView removeFromSuperview];
            [ws.calculateFormulaView removeFromSuperview];
        };
    }
    return _calculateFormulaView;
}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.tabBarController.view.bounds];
        _bgView.backgroundColor = [UIColor blackColor];
        _bgView.alpha = 0.4;
    }
    return _bgView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
